﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

using System.IO;
using System.Web;

namespace ScrumTable.Common
{
    #region Usings

    using System;
    using ScrumTable.Common.Properties;

    #endregion

    /// <summary>
    /// Contains math methods
    /// </summary>
    public static class MathUtil
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------


        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
        /// <summary>
        /// Gets the x/y pos on a ellipse
        /// circle = radiusX == radiusY
        /// </summary>
        /// <param name="numberOfEllipsePoints">numbers of point on the circle</param>
        /// <param name="targetPointOnCircle">target point on the circle</param>
        /// <param name="radiusX"></param>
        /// <param name="radiusY"></param>
        /// <param name="posX"></param>
        /// <param name="posY"></param>
        public static void GetEllipsePos(double numberOfEllipsePoints, int targetPointOnCircle, double radiusX, double radiusY, out double posX, out double posY)
        {
            posX = radiusX + radiusX * Math.Cos(Math.PI * 2 * targetPointOnCircle / numberOfEllipsePoints - Math.PI / 2);
            posY = radiusY + radiusY * Math.Sin(Math.PI * 2 * targetPointOnCircle / numberOfEllipsePoints - Math.PI / 2);
        }

        /// <summary>
        /// Gets the x/y pos on a rectlange
        /// square = xWidth == yHeight
        /// </summary>
        /// <param name="numberOfPoints">numbers of points on the rectangle</param>
        /// <param name="targetPoint">get the position for this point on the rectangle</param>
        /// <param name="xWidth">rectangle width</param>
        /// <param name="yHeight">rectangle height</param>
        /// <param name="itemHeight">height of the item</param>
        /// <param name="itemWidth">widgth of the item</param>
        /// <param name="posX"></param>
        /// <param name="posY"></param>
        public static void GetRectanglePos(double numberOfPoints, int targetPoint, double xWidth, double yHeight, double itemHeight, double itemWidth, out double posX, out double posY)
        {
            double margin = 0;
            xWidth -= itemWidth;
            bool isEvenNumbers = (numberOfPoints % 2 == 0);

            double half = Math.Floor(numberOfPoints/2);
            if (half > targetPoint)
            {
                posX = margin + xWidth / (half) * targetPoint ;
                posX = isEvenNumbers ?  posX : posX + targetPoint * itemWidth/half;
                posY = margin;
            }
            else
            {
                posX = margin + xWidth / (half) * (targetPoint-half); 
                posY = yHeight - margin - itemHeight;
            }
            
        }

        /// <summary>
        /// Checks if the value is between the two borders (min, max) and returns the value if ok otherwise one of the two borders
        /// </summary>
        /// <param name="value"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static double MinMax(this double value, double min, double max)
        {
            return Math.Min(Math.Max(value, min), max);
        }

        #endregion
    }
}
